/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */
/*
 *  (C) Victria Ltd. 2007
 */
package victria.marketsim.bot;

import java.io.Serializable;

import victria.marketsim.core.Order;

import cern.jet.random.AbstractDistribution;

public class RandomBot extends AbstractBot implements Serializable {
	
	protected Order.Direction direction;
	
	protected AbstractDistribution priceDistribution;
	
	protected AbstractDistribution quantityDistribution;
	
	protected AbstractDistribution sleepDistribution;
	
	protected int quantityMultiplier = 1;

	public RandomBot() {
		super();
	}
	
	public void placeOrder() {
		Order order = new Order();
		order.setDirection(direction);
		order.setPrice((float) Math.floor(priceDistribution.nextDouble()*100)/100);
		order.setQuantity(quantityDistribution.nextInt() * quantityMultiplier);
		logger.debug("Random bot placing order:" + order);
		placeOrder(order);
	}

	public void run() {
		stopped = false;
		while (!stopped) {
			sleep();
			placeOrder();
		}
	}

	public void sleep() {
		try {
			Thread.sleep(sleepDistribution.nextInt());
		} catch (InterruptedException e) {
			logger.debug(e);
			Thread.currentThread().interrupt();
		}
	}

	public Order.Direction getDirection() {
		return direction;
	}

	public void setDirection(Order.Direction direction) {
		this.direction = direction;
	}

	public AbstractDistribution getPriceDistribution() {
		return priceDistribution;
	}

	public void setPriceDistribution(AbstractDistribution priceDistribution) {
		this.priceDistribution = priceDistribution;
	}

	public AbstractDistribution getQuantityDistribution() {
		return quantityDistribution;
	}

	public void setQuantityDistribution(AbstractDistribution quantityDistribution) {
		this.quantityDistribution = quantityDistribution;
	}

	public AbstractDistribution getSleepDistribution() {
		return sleepDistribution;
	}

	public void setSleepDistribution(AbstractDistribution sleepDistribution) {
		this.sleepDistribution = sleepDistribution;
	}

	public int getQuantityMultiplier() {
		return quantityMultiplier;
	}

	public void setQuantityMultiplier(int quantityMultiplier) {
		this.quantityMultiplier = quantityMultiplier;
	}

	public String toString() {
		return "(" + getClass() + " botName:" + botId
				+ " quantityMulitplier:" + quantityMultiplier
				+ " quantityDistribution:" + quantityDistribution
				+ " priceDistribution:" + priceDistribution
				+ " sleepDistribution:" + sleepDistribution + ")";
	}
}
